package com.example.keen1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class CalculateActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
    EditText et1,et2;
    TextView addTv,jianTv,subTv,divTv;
    CheckBox addCb,jianCb,subCb,divCb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);
        initView();
        //设置监听器
        addCb.setOnCheckedChangeListener(this);
        jianCb.setOnCheckedChangeListener(this);
        subCb.setOnCheckedChangeListener(this);
        divCb.setOnCheckedChangeListener(this);
    }
    private void startCal(boolean isChecked,int fuhao) {
        if (isChecked) {
            String s1 = et1.getText().toString();
            String s2 = et2.getText().toString();
            if (TextUtils.isEmpty(s1)||TextUtils.isEmpty(s2)) {
                Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
                return;
            }
            int i1 = Integer.parseInt(s1);
            int i2 = Integer.parseInt(s2);
            switch (fuhao) {
                case 1:
                    int res = i1+i2;
                    String msg = i1+" + "+i2+" = "+res;
                    addTv.setText(msg);
                    addTv.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    res = i1-i2;
                    msg = i1+" - "+i2+" = "+res;
                    jianTv.setText(msg);
                    jianTv.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    res = i1*i2;
                    msg = i1+" * "+i2+" = "+res;
                    subTv.setText(msg);
                    subTv.setVisibility(View.VISIBLE);
                    break;
                case 4:
                    res = i1/i2;
                    msg = i1+" / "+i2+" = "+res;
                    divTv.setText(msg);
                    divTv.setVisibility(View.VISIBLE);
                    break;
            }
        }else {
            switch (fuhao) {
                case 1:addTv.setVisibility(View.INVISIBLE);break;
                case 2:jianTv.setVisibility(View.INVISIBLE);break;
                case 3:subTv.setVisibility(View.INVISIBLE);break;
                case 4:divTv.setVisibility(View.INVISIBLE);break;
            }
        }
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_add:
                startCal(isChecked,1);
                break;
            case R.id.cb_jian:
                startCal(isChecked,2);
                break;
            case R.id.cb_sub:
                startCal(isChecked,3);
                break;
            case R.id.cb_div:
                startCal(isChecked,4);
                break;
        }
    }



    private void initView() {
        et1 = findViewById(R.id.et1);
        et2 = findViewById(R.id.et2);
        addTv = findViewById(R.id.tv_add);
        jianTv = findViewById(R.id.tv_jian);
        subTv = findViewById(R.id.tv_sub);
        divTv = findViewById(R.id.tv_div);
        addCb = findViewById(R.id.cb_add);
        jianCb = findViewById(R.id.cb_jian);
        subCb = findViewById(R.id.cb_sub);
        divCb = findViewById(R.id.cb_div);
        //不显示结果信息
        addTv.setVisibility(View.INVISIBLE);
        jianTv.setVisibility(View.INVISIBLE);
        subTv.setVisibility(View.INVISIBLE);
        divTv.setVisibility(View.INVISIBLE);
    }


}
